package com.prs.crm.action.oa;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.domain.oa.PictureNews;
import com.prs.crm.domain.oa.PictureNewsCollect;
import com.prs.crm.domain.oa.PictureNewsCollectType;
import com.prs.crm.domain.oa.PictureNewsReceive;
import com.prs.crm.service.oa.PictureNewsCollectService;
import com.prs.crm.service.oa.PictureNewsCollectTypeService;
import com.prs.crm.service.oa.PictureNewsReceiveService;
import com.prs.crm.service.oa.PictureNewsService;

public class PictureNewsCollectAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private PaginationSupport pagination;
	private PictureNews pictureNews;
	private PictureNewsService pictureNewsService;
	private PictureNewsCollect pictureNewsCollect;
	private PictureNewsCollectService pictureNewsCollectService;
	private PictureNewsCollectType pictureNewsCollectType;
	private PictureNewsCollectTypeService pictureNewsCollectTypeService;
	private Integer[] collectIds;
	private PictureNewsReceive pictureNewsReceive;
	private PictureNewsReceiveService pictureNewsReceiveService;
	@Action("listPictureNewsCollect")
	public String listPictureNewsReceive() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				PictureNewsCollect.class);
//		criteria.createAlias("pictureNews", "pn");	
//		criteria.createAlias("pictureNewsReceive", "pns");	
//		criteria.add(Restrictions.eq("pn.oabox", 3));
//		criteria.add(Restrictions.eq("pns.oabox", 3));
//		criteria.add(Restrictions.or(Restrictions.eq("pn.oabox", 3), Restrictions.eq("pns.oabox", 3)));
		criteria.add(Restrictions.eq("recorder.id", AppContext.getAccountContext().currentUser().getId()));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.pictureNewsCollectService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action(value = "savePictureNewsCollect", results = {
			@Result(name = "success", type = "redirect", location = "listPictureNews"),
			@Result(name = "error", type = "chain", location = "viewPictureNews?pictureNews=${pictureNews.getId()}") })
	public String savePictureNewsCollect() {
		this.pictureNews.setOabox(3);
		this.pictureNewsService.submit(pictureNews);
		PictureNewsCollect pc=new PictureNewsCollect();
		pc.setPictureNews(pictureNews);
		PictureNewsCollectType p=this.pictureNewsCollectTypeService.getTypeName("已发新闻");
		if(p==null){
			p=new PictureNewsCollectType();
			p.setName("已发新闻");
			this.pictureNewsCollectTypeService.save(p);
		}
		pc.setType(p);
		this.pictureNewsCollectService.save(pc);
		return SUCCESS;
	}
	@Action(value = "savePictureNewsReceiveCollect", results = {
			@Result(name = "success", type = "redirect", location = "listPictureNewsReceive"),
			@Result(name = "error", type = "chain", location = "viewPictureNews?pictureNews=${pictureNews.getId()}") })
	public String savePictureNewsReceiveCollect() {
		PictureNewsCollect pc=new PictureNewsCollect();
		pictureNewsReceive.setOabox(3);
		pc.setPictureNewsReceive(pictureNewsReceive);
		PictureNewsCollectType p=this.pictureNewsCollectTypeService.getTypeName("已收新闻");
		if(p==null){
			p=new PictureNewsCollectType();
			p.setName("已收新闻");
			this.pictureNewsCollectTypeService.save(p);
		}
		pc.setType(p);
		this.pictureNewsCollectService.save(pc);
		return SUCCESS;
	}
	@Action(value = "removePictureNewsCollect", results = {
			@Result(name = "success", type = "redirect", location = "listPictureNewsCollect"),
			@Result(name = "error", type = "chain", location = "listPictureNewsCollect") })
	public String removePictureNewsCollect() {
		this.pictureNewsCollectService.batchRemove(collectIds);
		return SUCCESS;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PictureNews getPictureNews() {
		return pictureNews;
	}

	public void setPictureNews(PictureNews pictureNews) {
		this.pictureNews = pictureNews;
	}

	public PictureNewsService getPictureNewsService() {
		return pictureNewsService;
	}

	public void setPictureNewsService(PictureNewsService pictureNewsService) {
		this.pictureNewsService = pictureNewsService;
	}

	public PictureNewsCollect getPictureNewsCollect() {
		return pictureNewsCollect;
	}

	public void setPictureNewsCollect(PictureNewsCollect pictureNewsCollect) {
		this.pictureNewsCollect = pictureNewsCollect;
	}

	public PictureNewsCollectService getPictureNewsCollectService() {
		return pictureNewsCollectService;
	}

	public void setPictureNewsCollectService(
			PictureNewsCollectService pictureNewsCollectService) {
		this.pictureNewsCollectService = pictureNewsCollectService;
	}
	public PictureNewsCollectType getPictureNewsCollectType() {
		return pictureNewsCollectType;
	}
	public void setPictureNewsCollectType(
			PictureNewsCollectType pictureNewsCollectType) {
		this.pictureNewsCollectType = pictureNewsCollectType;
	}
	public PictureNewsCollectTypeService getPictureNewsCollectTypeService() {
		return pictureNewsCollectTypeService;
	}
	public void setPictureNewsCollectTypeService(
			PictureNewsCollectTypeService pictureNewsCollectTypeService) {
		this.pictureNewsCollectTypeService = pictureNewsCollectTypeService;
	}

	public Integer[] getCollectIds() {
		return collectIds;
	}

	public void setCollectIds(Integer[] collectIds) {
		this.collectIds = collectIds;
	}

	public PictureNewsReceive getPictureNewsReceive() {
		return pictureNewsReceive;
	}

	public void setPictureNewsReceive(PictureNewsReceive pictureNewsReceive) {
		this.pictureNewsReceive = pictureNewsReceive;
	}

	public PictureNewsReceiveService getPictureNewsReceiveService() {
		return pictureNewsReceiveService;
	}

	public void setPictureNewsReceiveService(
			PictureNewsReceiveService pictureNewsReceiveService) {
		this.pictureNewsReceiveService = pictureNewsReceiveService;
	}
	
}
